﻿#pragma execution_character_set("utf-8")
#include "database.h"
#include "supplierwidget.h"
#include "ui_supplierwidget.h"

SupplierWidget::SupplierWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SupplierWidget)
{
    ui->setupUi(this);
    selectrow = 0;
    QStringList header;
    header <<"名称"<<"地址"<<"电话"<<"法人"<<"邮箱";
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(50);
    updateDisplay();
    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)),this,SLOT(onHeaderClicked(int,int)));
}

SupplierWidget::~SupplierWidget()
{
    delete ui;
}


void SupplierWidget::showEvent(QShowEvent *event)
{
    updateDisplay();
    ui->tableWidget->horizontalHeader()->resizeSection(0,ui->tableWidget->width()*0.2);
    ui->tableWidget->horizontalHeader()->resizeSection(1,ui->tableWidget->width()*0.2);
    ui->tableWidget->horizontalHeader()->resizeSection(2,ui->tableWidget->width()*0.2);
    ui->tableWidget->horizontalHeader()->resizeSection(3,ui->tableWidget->width()*0.2);
    ui->tableWidget->horizontalHeader()->resizeSection(4,ui->tableWidget->width()*0.2);
}

void SupplierWidget::updateDisplay()
{
    ui->tableWidget->clearContents();
    QVector<QVector<QString>>  allinfo = DataBasePri::queryAll("supplier");
    ui->tableWidget->setRowCount(allinfo.size());
    for(int i =0;i < allinfo.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(allinfo.at(i).at(0)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(allinfo.at(i).at(1)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(allinfo.at(i).at(2)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(allinfo.at(i).at(3)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(allinfo.at(i).at(4)));
    }
}

void SupplierWidget::onHeaderClicked(int row,int column)
{
    ui->lineEdit_a->setText(ui->tableWidget->item(row,0)->text());
    ui->lineEdit_b->setText(ui->tableWidget->item(row,1)->text());
    ui->lineEdit_c->setText(ui->tableWidget->item(row,2)->text());
    ui->lineEdit_d->setText(ui->tableWidget->item(row,3)->text());
    ui->lineEdit_e->setText(ui->tableWidget->item(row,4)->text());
}

void SupplierWidget::on_pushButton_1_clicked()
{
    QString lineEdit_a = ui->lineEdit_a->text();//
    QString lineEdit_b = ui->lineEdit_b->text();//
    QString lineEdit_c = ui->lineEdit_c->text();//
    QString lineEdit_d = ui->lineEdit_d->text();//
    QString lineEdit_e = ui->lineEdit_e->text();//
    if(lineEdit_a == "" || lineEdit_b == ""){//判断是否为空
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("用户名或密码不能为空");
        msgBox.exec();
    }else{
        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        QSqlQuery query(DataBasePri::database);//创建数据库请求对象
        QString t_str = QString::fromLocal8Bit("insert into supplier values('%1','%2','%3','%4','%5')")
                .arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e);
        bool ret = query.exec(t_str);
        qDebug()<<t_str;
        DataBasePri::database.close();
        if(ret == true){
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("添加成功");
            msgBox.exec();
            updateDisplay();
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("添加失败");
            msgBox.exec();
        }
        updateDisplay();//刷新界面
    }
}

void SupplierWidget::on_pushButton_2_clicked()
{
    QString lineEdit_a = ui->lineEdit_a->text();//获取界面输入电话
    QString lineEdit_b = ui->lineEdit_b->text();//获取界面输入姓名
    QString lineEdit_c = ui->lineEdit_c->text();//获取界面输入密码
    QString lineEdit_d = ui->lineEdit_d->text();//获取界面输入
    QString lineEdit_e = ui->lineEdit_e->text();//获取界面输入

    if(lineEdit_a == "" || lineEdit_b == ""){//判断输入情况
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("用户名或密码不能为空");
        msgBox.exec();
    }else{
        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        QSqlQuery query(DataBasePri::database);
        bool ret =  query.exec(QString::fromLocal8Bit("update supplier set place = '%1' where name = '%2'").arg(lineEdit_b).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update supplier set phone = '%1' where name = '%2'").arg(lineEdit_c).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update supplier set legalerson = '%1' where name = '%2'").arg(lineEdit_d).arg(lineEdit_a));;
        ret =  query.exec(QString::fromLocal8Bit("update supplier set email = '%1' where name = '%2'").arg(lineEdit_e).arg(lineEdit_a));;

        DataBasePri::database.close();
        if(ret == true){
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("修改成功");
            msgBox.exec();
            updateDisplay();
        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("修改失败");
            msgBox.exec();
        }
        updateDisplay();
    }
}

void SupplierWidget::on_pushButton_3_clicked()
{
    QTableWidgetItem *t_item = ui->tableWidget->item(selectrow,0);//当前选择哪一行
    QString t_text = t_item->text();
    qDebug()<<"t_item"<<t_text;
    if (! DataBasePri::database.open()){//打开数据库
        qCritical() << DataBasePri::database.lastError().text();
        return;
    }
    QSqlQuery query(DataBasePri::database);//数据库请求对象
    bool ret = query.exec(QString::fromLocal8Bit("delete from supplier where name = '%1'").arg(t_text));
    qDebug()<<ret;
    if(ret == true){
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("删除成功");
        msgBox.exec();
        updateDisplay();
    }else{
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("删除失败");
        msgBox.exec();
    }
    DataBasePri::database.close();//关闭数据库
    updateDisplay();//刷新界面
}

void SupplierWidget::on_pushButton_4_clicked()
{
    emit sendDisWidget("m_AdminWidget");
}
